package com.enjoyor.soa.traffic.ping.efoms.dubbo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.enjoyor.soa.traffic.ping.efoms.PingServerApplication;
import com.enjoyor.soa.traffic.ping.efoms.dubbo.invoke.DubboDeviceInfoInvoke;
import com.enjoyor.soa.traffic.ping.efoms.helper.CheckPojoHelper;
import com.enjoyor.soa.traffic.ping.efoms.pojo.PingResultPojo;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

/**
 * 获取Ip，同时默认Ip的发包次数为10次，
 * 后续对于丢包100%的ip降低Ip的发包次数
 * 所有的ip与发包次数存储在IPPool中key-value:ip-times(IP:发包次数)
 * @ClassNameIPCategoryService
 * @Description TODO
 * @Author ZSY
 * @Date 2019/3/1 9:11
 * @Version 1.0
 **/
@Component
public class IPCategoryService {
	
	@Value("${ping.device.category}")
    private String deviceCategory;
	
	@Value("${ping.device.types}")
    private String deviceTypes;
	
    @Value("${ping.times.heigh}")
    private int heighTimes;

    @Value("${ping.tiemout}")
    private int timeout;
    
    @Value("${ping.server.isTest}")
    private Boolean pingIsTest;
    
    @Value("${ping.start.index}")
    private int pingListStart;
    
    @Value("${ping.end.index}")
    private int pingListEnd;
	
	@Autowired
	private Query query;
    
    //存储IP与发包数量
    public static final Map<String, PingResultPojo> IPPool = new ConcurrentHashMap<>();

    private DubboDeviceInfoInvoke getDeviceInfoService() {
    	DubboDeviceInfoInvoke service = PingServerApplication.context.getBean(DubboDeviceInfoInvoke.class);
		return service;
	}
    /**
     * 通过接口获取ip列表
     *
     * @return
     */
    public List<String> getIPList() {
        List list = new ArrayList<>();
        for (int i = 1; i < 255; i++) {
            list.add("192.168.56." + i);
        }
        //TODO 由第三方提供相关接口获取对应IP列表
        return list;
    }

    /**
     * 获取IPPool
     *
     * @return
     * @throws Exception 
     */
    public Map<String, PingResultPojo> getIPMapPool() throws Exception {
        if (IPPool == null||IPPool.size()==0) {
            initIpPool();
        }
        return IPPool;
    }

    /**
     * 初始化IPPool,并定时更新相关IPPool内的数据
     */
    @Scheduled(cron="0 */${ping.device.period} * * * ?")
    @SuppressWarnings("unchecked")
    private void initIpPool() throws Exception{
    	IPPool.clear();
    	ResultPojo resultPojo  = query.getPingResultPojoList();
        	if(CheckPojoHelper.isValid(resultPojo)) {
        		List<PingResultPojo> PingResultPojoList = (List<PingResultPojo>)resultPojo.getResultList();
        		for(int j = pingListStart;j<pingListEnd;j++){
        			if(j<=(PingResultPojoList.size()-1)){
        				String Ip = PingResultPojoList.get(j).getIP();
        				String key = "kk" + "-" + PingResultPojoList.get(j).getDevId();
        				PingResultPojo pingPojo = IPPool.get(key);
        				if(!pingIsTest) { 
        					if(null == pingPojo) {
        						IPPool.put(key, PingResultPojoList.get(j));
        					}else {
        						if((null == Ip && null != pingPojo.getIP()) || (null != Ip && !Ip.equals(pingPojo.getIP()))) {
        							IPPool.put(key, pingPojo);
        						}
        						if(pingPojo.getTimes() == 0 || pingPojo.getTimes() > heighTimes) {
        							pingPojo.setTimes(heighTimes);
        							IPPool.put(key, pingPojo);
        						}
        					} 
        				}        				
        			}
        		}
        	}
    }
}
